﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DotNet.Random.IdCard
{
    /// <summary>
    /// 18位身份证随机类，实现了接口IStringRandService
    /// </summary>
    public class IdCardRandService : DotNet.Random.Interface.IStringRandService
    {
        /// <summary>
        /// Rands this instance.
        /// 随机生成字符串方法
        /// 映射到虚方法中，扩展时，可重写
        /// </summary>
        /// <returns>
        /// 返回字符串类型
        /// </returns>
        /// <exception cref="System.Exception">address is null</exception>
        public virtual string Rand()
        {
            var res = "";

            #region address rand

            var addressRand = new StatisticsAddressRandService();
            var address = addressRand.Rand();
            if (address == null)
            {
                throw new Exception("address is null");
            }
            res += address.num;

            #endregion

            #region date rand

            var minTime = new System.DateTime(1900, 1, 1);
            var maxTime = System.DateTime.Now;
            var dateRand = RandService.RandDateTime(minTime, maxTime);
            res += dateRand.ToString("yyyyMMdd");

            #endregion

            #region rand three

            var intRand = new Int.TenNumberRandService();
            for (int i = 0; i < 3; i++)
            {
                res += intRand.Rand();
                System.Threading.Thread.Sleep(200);
            }

            #endregion

            #region last

            var lastRand = new IdCardLastLetterRandService(res);
            res += lastRand.Rand();

            #endregion

            System.Threading.Thread.Sleep(200);

            return res;
        }
    }
}
